package com.ruoyi.system.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.SysPhoto;
import com.ruoyi.system.domain.SysWorks;
import com.ruoyi.system.mapper.SysWorksMapper;
import com.ruoyi.system.service.ISysPhotoService;
import com.ruoyi.system.service.ISysVoteService;
import com.ruoyi.system.service.ISysWorksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * 作品Service业务层处理
 *
 * @author ruoyi
 * @date 2023-10-30
 */
@Service
public class SysWorksServiceImpl implements ISysWorksService {
    @Autowired
    private ISysPhotoService sysPhotoService;
    @Autowired
    private SysWorksMapper sysWorksMapper;
    @Autowired
    private ISysVoteService sysVoteService;

    /**
     * 查询作品
     *
     * @param worksKey 作品主键
     * @return 作品
     */
    @Override
    public SysWorks selectSysWorksByWorksKey(String worksKey) {
        int count = sysVoteService.countByWorksKey(worksKey);
        SysWorks sysWorks = sysWorksMapper.selectSysWorksByWorksKey(worksKey);
        sysWorks.setWorksVote(String.valueOf(count));
        return sysWorks;
    }

    /**
     * 查询作品列表
     *
     * @param sysWorks 作品
     * @return 作品
     */
    @Override
    public List<SysWorks> selectSysWorksList(SysWorks sysWorks) {
        List<SysWorks> works = sysWorksMapper.selectSysWorksList(sysWorks);
        for (int i = 0; i < works.size(); i++) {
            int count = sysVoteService.countByWorksKey(works.get(i).getWorksKey());
            works.get(i).setWorksVote(String.valueOf(count));
        }
        return works;
    }

    /**
     * 新增作品
     *
     * @param sysWorks 作品
     * @return 结果
     */
    @Override
    public int insertSysWorks(SysWorks sysWorks) {
        sysWorks.setWorksKey(UUID.randomUUID().toString());
        SysUser user = SecurityUtils.getLoginUser().getUser();
        sysWorks.setWorksUserId(user.getUserId());
        sysWorks.setWorksUserName(user.getNickName());
        sysWorks.setLoginIp(user.getLoginIp());
        sysWorks.setProvinces(user.getProvinces());
        sysWorks.setCity(user.getCity());
        sysWorks.setCounty(user.getCounty());
        sysWorks.setAddress(user.getAddress());
        LoginUser loginUser = (LoginUser) SecurityUtils.getAuthentication().getPrincipal();
        Long userId = loginUser.getUser().getUserId();
        sysWorks.setCreateBy(userId.toString());
        sysWorks.setCreateTime(DateUtils.getNowDate());
        // todo
        insert(sysWorks.getWorksKey(), sysWorks.getUrl1());
        insert(sysWorks.getWorksKey(), sysWorks.getUrl2());
        insert(sysWorks.getWorksKey(), sysWorks.getUrl3());
        insert(sysWorks.getWorksKey(), sysWorks.getUrl4());
        return sysWorksMapper.insertSysWorks(sysWorks);
    }

    private void insert(String worksKey, String url) {
        if (url == null || "".equals(url)) {
            return;
        }
        SysPhoto sysPhoto = new SysPhoto();
        sysPhoto.setWorksKey(worksKey);
        sysPhoto.setPhotoUrl(url);
        sysPhotoService.insertSysPhoto(sysPhoto);
    }


    /**
     * 修改作品
     *
     * @param sysWorks 作品
     * @return 结果
     */
    @Override
    public int updateSysWorks(SysWorks sysWorks) {
        LoginUser loginUser = (LoginUser) SecurityUtils.getAuthentication().getPrincipal();
        Long userId = loginUser.getUser().getUserId();
        sysWorks.setUpdateBy(userId.toString());
        sysWorks.setUpdateTime(DateUtils.getNowDate());
        return sysWorksMapper.updateSysWorks(sysWorks);
    }

    /**
     * 批量删除作品
     *
     * @param worksKeys 需要删除的作品主键
     * @return 结果
     */
    @Override
    public int deleteSysWorksByWorksKeys(String[] worksKeys) {
        return sysWorksMapper.deleteSysWorksByWorksKeys(worksKeys);
    }

    /**
     * 删除作品信息
     *
     * @param worksKey 作品主键
     * @return 结果
     */
    @Override
    public int deleteSysWorksByWorksKey(String worksKey) {
        return sysWorksMapper.deleteSysWorksByWorksKey(worksKey);
    }

    @Override
    public List<SysWorks> piaoshu(String worksUserId) {

        List<SysWorks> works = sysWorksMapper.piaoshu(worksUserId);
        // for (SysWorks work : works) {
        //     int count = sysVoteService.countByWorksKey(work.getWorksKey());
        //     work.setWorksVote(String.valueOf(count));
        // }
        for (int i = 0; i < works.size(); i++) {
            int count = sysVoteService.countByWorksKey(works.get(i).getWorksKey());
            works.get(i).setWorksVote(String.valueOf(count));
        }
        // f根据 worksvote排序 stream
        works.sort((o1, o2) -> Integer.parseInt(o2.getWorksVote()) - Integer.parseInt(o1.getWorksVote()));
        return works;
    }

    @Override
    public List<SysWorks> shijian() {
        List<SysWorks> works = sysWorksMapper.shijian();
        for (SysWorks work : works) {
            int count = sysVoteService.countByWorksKey(work.getWorksKey());
            work.setWorksVote(String.valueOf(count));
        }
        return works;
    }

    @Override
    public List<SysWorks> selectWorksList(SysWorks sysWorks) {
        return sysWorksMapper.selectWorksList(sysWorks);
    }
}
